﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class ApproveStockAdjustmentVoucher : System.Web.UI.Page
    {
        ApproveStockAdjustmentVoucherControl control;
        User user;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId < 6 || roleId > 7)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                control = new ApproveStockAdjustmentVoucherControl();

                List<string> userList = Session["UserLoggedIn"] as List<string>;
                string username = userList[3];
                user = control.GetUserByUsername(username);

                // Block the buttons to prevent multiple clicks by the user
                btnApprove.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnApprove).ToString() + ";");

                if (!IsPostBack)
                {
                    lblMessage.Text = "";
                    GenerateGridView();
                }
            }
            catch (Exception ex)
            {
                // Release block button
                btnApprove.Enabled = true;
            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {
                List<int> voucherList = Session["AdjustmentDetail"] as List<int>;

                // Update ApprovalBy, ApprovalDate and Status to database
                string redirectURL = control.UpdateVouchers(user, voucherList);

                // Redirect to successful page
                Response.Redirect(redirectURL);
            }
            catch (Exception ex)
            {
                // Release block button
                btnApprove.Enabled = true;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                string redirectURL = control.ReturnToStockAdjustmentVoucherList();
                Response.Redirect(redirectURL);
            }
            catch (Exception ex)
            {
            }
        }

        private void GenerateGridView()
        {
            List<int> voucherId = Session["AdjustmentDetail"] as List<int>;
            if (voucherId != null)
            {
                int voucherID = voucherId[0];
                List<AdjustmentDetail> list = control.DisplayAdjustmentDetail(voucherID);
                lblRequestedBy.Text = list[0].Adjustment.Employee.EmployeeName;
                lblRequestedDate.Text = Convert.ToDateTime(list[0].Adjustment.RequestedDate).ToShortDateString();
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].ItemCode = list[i].Stationery.ItemName;
                }
                gvAdjustDetail.DataSource = list.OrderBy(x => x.ItemCode);
                gvAdjustDetail.DataBind();
            }
            else
            {
                lblMessage.Text = "No stock adjustment voucher is selected";
                gvAdjustDetail.Visible = false;
                btnApprove.Visible = false;
                lblRequestedBy.Visible = false;
                lblRequestedByLabel.Visible = false;
                lblRequestedDate.Visible = false;
                lblRequestedDateLabel.Visible = false;
            }                            
        }
    }
}